package com.intelligentmeterreadingsys.settingsys.activity;

import com.intelligentmeterreadingsys.MainActivity;
import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.util.SharedPreferenceUtil;

import dbhelper.XmlNameHelper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 设置安全密码
 * @author king
 *
 */
public class SettingBasicsActivity extends Activity implements OnFocusChangeListener,OnClickListener{
   
	public Button button1;//确定
	
	public Button button2;//返回
	
	public Button button3;//修改安全时间
	
	public EditText edit1;//原密码
	
	public EditText edit2;//新密码
	
	public EditText edit3;//二次密码
	
	public EditText edit4;//安全时间 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.setting_basics);
		init();
	}
	
	/**
	 * 初始化控件
	 */
	public void init(){
		button1=(Button)findViewById(R.id.setting_basic_ok);
		button2=(Button)findViewById(R.id.setting_basic_clean);
		edit1=(EditText)findViewById(R.id.setting_oldpassword);
		edit2=(EditText)findViewById(R.id.setting_newpassword);
		edit3=(EditText)findViewById(R.id.setting_towpassword);
		edit4=(EditText)findViewById(R.id.setting_safetytime);
		button3=(Button)findViewById(R.id.setting_modiey_pass);
		SharedPreferenceUtil sp=new SharedPreferenceUtil(this);
		edit4.setText(sp.getValue(XmlNameHelper.safetytime));
		edit1.setOnFocusChangeListener(this);
		edit2.setOnFocusChangeListener(this);
		edit3.setOnFocusChangeListener(this);
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		button3.setOnClickListener(this);
		
	}
	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		SharedPreferenceUtil sp=new SharedPreferenceUtil(SettingBasicsActivity.this);
		
		switch (v.getId()) {
		case R.id.setting_oldpassword:
			 if(!hasFocus){
				 String pass=edit1.getEditableText().toString();
				 String pass1=sp.getValue(XmlNameHelper.passWord);
				 if(!pass.equals(pass1)){
					 edit1.setText("");
					 Toast.makeText(SettingBasicsActivity.this, "密码错误！",Toast.LENGTH_LONG).show();
				 }
			 }
			break;
		case R.id.setting_towpassword:
			
			if(!hasFocus){
				String pass1=edit2.getEditableText().toString();
				String pass2=edit3.getEditableText().toString();
				if(!pass1.equals(pass2)){
					 Toast.makeText(SettingBasicsActivity.this, "两次密码输入有误！",Toast.LENGTH_LONG).show();
					 edit3.setText("");
				}
				}
			
			break;
		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		SharedPreferenceUtil sp=new SharedPreferenceUtil(SettingBasicsActivity.this);
		String password=sp.getValue(XmlNameHelper.passWord);
		if(v==button1){
			String pass=edit2.getEditableText().toString();
			String pass1=edit3.getEditableText().toString();
			if(!pass.equals(pass1)){
				 Toast.makeText(SettingBasicsActivity.this, "两次密码输入有误！",Toast.LENGTH_LONG).show();
				 return;
			}
			if(!edit1.getEditableText().toString().equals(password)){
				 Toast.makeText(SettingBasicsActivity.this, "密码错误！",Toast.LENGTH_LONG).show();
				 return;
			}
			Toast.makeText(SettingBasicsActivity.this, "修改成功！",Toast.LENGTH_LONG).show();
			sp.insertValue(XmlNameHelper.passWord, pass);
			  String safetime=sp.getValue(XmlNameHelper.safetytime);
		        if(safetime==null||safetime.equals("")){
		        	 sp.insertValue(XmlNameHelper.isSafe, true);
		        }else{
		        	 sp.insertValue(XmlNameHelper.isSafe, false);
		        }
		}else if(v==button3){
		boolean b=	 sp.insertValue(XmlNameHelper.safetytime, edit4.getEditableText().toString());
		if(b){
			 Toast.makeText(SettingBasicsActivity.this, "修改成功！",Toast.LENGTH_LONG).show();
		}else{
			Toast.makeText(SettingBasicsActivity.this, "修改失败！",Toast.LENGTH_LONG).show();
		}
		}
		else{
			Intent intent=new Intent(SettingBasicsActivity.this, MainActivity.class);
			startActivity(intent);
		}
		
	}
	

    
	
}
